define(['app'], function (app) {
	'use strict';

	var controller = function controller($scope, Unsalablepro, GoPage,GoDialog, $window, GoAddress, GoCatTypeAuth, $timeout, $ionicSideMenuDelegate) {
		$scope.input = new Object();
		$scope.input.keywords = '';
		$scope.order = 1;
		$scope.region = '';
		$scope.store_cat = '';
		$scope.input.brand1 = '';
		$scope.input.brand2 = '';
		$scope.input.brand3 = '';
		$scope.listData = new Object();
		$scope.input.nPage = 1;

		//画面加载
		$scope.$on('$ionicView.afterEnter', function () {
			var lan = 0; //这是状态栏，只有ios的APP才有
			if (window.cordova && window.cordova.plugins) {
				if (ionic.Platform.isIOS()) {
					lan = 20;
				}
			}
			var all = $window.innerHeight;
			var orderBar = document.getElementById("orderBar").clientHeight;
			$scope.unsalable_myObj = {
				"height": all - 44 - lan - orderBar + "px"
			};
		}, false)

		//侧边菜单
		$scope.rightSideMenu = function () {
			$ionicSideMenuDelegate.toggleRight();
		};

		/***省市区三级联动***/
		$scope.input.province = { id: 0, name: "请选择省份" }; //省市区三级联动
		$scope.input.city = { id: 0, name: "请选择城市" }; //省市区三级联动
		$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动
		var items = new Array();
		var obj1 = { name: 'input.province' };
		var obj2 = { name: 'input.city' };
		var obj3 = { name: 'input.district' };
		items.push(obj1);
		items.push(obj2);
		items.push(obj3);
		GoAddress.init_load($scope, items);

		$scope.doSelectProvince = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择省份", $scope.province_items, $scope.input.province, function (data) {
				$scope.input.province = data;
			});
		}
		$scope.doSelectCity = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择城市", $scope.city_items, $scope.input.city, function (data) {
				$scope.input.city = data;
			});
		}
		$scope.doSelectDistrict = function () {
			//id是值 name是显示
			GoDialog.showSelect("请选择区/县", $scope.district_items, $scope.input.district, function (data) {
				$scope.input.district = data;
			});
		}
		/***分类三级联动***/
		$scope.input.cat1 = { id: 0, name: "请选择大分类" };
		$scope.input.cat2 = { id: 0, name: "请选择中分类" };
		$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		//下拉侦听
		$scope.listen_cat1 = function (data) {
			$scope.input.cat2 = { id: 0, name: "请选择中分类" };
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
			$scope.cat3_items = '';
		};
		$scope.listen_cat2 = function (data) {
			$scope.input.cat3 = { id: 0, name: "请选择小分类" };
		};
		$scope.input.brand_id = "0"; //这是品牌下拉框的绑定数据
		$scope.count = null;
		//定义绑定
		var items_cat = new Array();
		var obj1_cat = { name: 'input.cat1' };
		var obj2_cat = { name: 'input.cat2' };
		var obj3_cat = { name: 'input.cat3' };
		items_cat.push(obj1_cat);
		items_cat.push(obj2_cat);
		items_cat.push(obj3_cat);
		GoCatTypeAuth.init_load_only($scope, items_cat);
		$scope.doSelectCat1 = function () {
			//id是值 name是显示
			GoDialog.showSelect("大分类", $scope.cat1_items, $scope.input.cat1, function (data) {
				$scope.input.cat1 = data;
			});
		}
		$scope.doSelectCat2 = function () {
			//id是值 name是显示
			GoDialog.showSelect("中分类", $scope.cat2_items, $scope.input.cat2, function (data) {
				$scope.input.cat2 = data;
			});
		}
		$scope.doSelectCat3 = function () {
			//id是值 name是显示
			GoDialog.showSelect("小分类", $scope.cat3_items, $scope.input.cat3, function (data) {
				$scope.input.cat3 = data;
			});
		}
		$scope.isLoad = false;
		//下拉侦听，目前只能省市区三级联动
		$scope.listen_province = function (data) {
			$scope.input.city = { id: 0, name: "请选择城市" }; //省市区三级联动
			$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动
			$scope.district_items = null;
		};
		$scope.listen_city = function (data) {
			$scope.input.district = { id: 0, name: "请选择区/县" }; //省市区三级联动
		};

		//加载数据
		$scope.init_data = function () {
			Unsalablepro.get_list(1, $scope.input.keywords, $scope.order, $scope.region, $scope.store_cat, function (data) {
				if (data.data.length > 0) {
					$scope.listData = data;
				} else
					$scope.listData = new Object();
				$scope.input.nPage = 1;
			});
		}
		$scope.init_data();

		//搜索
		$scope.doSearch = function () {
			//这里要重新获取地区和品类
			$scope.get_brand();
			$scope.get_region();
			Unsalablepro.get_list(1, $scope.input.keywords, $scope.order, $scope.region, $scope.store_cat, function (data) {
				if (data.data.length > 0) {
					$scope.listData = data;
				} else
					$scope.listData = new Object();
				$scope.input.nPage = 1;
			});
		}

		//排序--价格
		$scope.price_order = function () {
			if ($scope.order == 4) {
				$scope.order = 5;
			} else {
				$scope.order = 4;
			}
			console.log($scope.order)
			Unsalablepro.get_list(1, $scope.input.keywords, $scope.order, $scope.region, $scope.store_cat, function (data) {
				if (data.data.length > 0) {
					$scope.listData = data;
				} else
					$scope.listData = new Object();
				$scope.input.nPage = 1;
			});
		}
		//排序--默认
		$scope.default_order = function () {
			$scope.order = 1;
			Unsalablepro.get_list(1, $scope.input.keywords, $scope.order, $scope.region, $scope.store_cat, function (data) {
				if (data.data.length > 0) {
					$scope.listData = data;
				} else
					$scope.listData = new Object();
				$scope.input.nPage = 1;
			});
		}

		$scope.get_brand = function () {
			$scope.store_cat = '';
			if ($scope.input.cat1) {
				$scope.store_cat = $scope.input.cat1.id;
			}
			if ($scope.input.cat2) {
				$scope.store_cat += ',' + $scope.input.cat2.id;
			}
			if ($scope.input.cat3) {
				$scope.store_cat += ',' + $scope.input.cat3.id;
			}
		}
		$scope.get_region = function () {
			$scope.region = '';
			if ($scope.input.province) {
				$scope.region = $scope.input.province.id;
			}
			if ($scope.input.city) {
				$scope.region = $scope.input.province.id + ',' + $scope.input.city.id;
			}
			if ($scope.input.district) {
				$scope.region = $scope.input.province.id + ',' + $scope.input.city.id + ',' + $scope.input.district.id;
			}
		}

		//下拉刷新 判断哪个模块进行刷新
		$scope.doRefresh = function () {
			$scope.isLoad = true;
			Unsalablepro.get_list(1, $scope.input.keywords, $scope.order, $scope.region, $scope.store_cat, function (data) {
				if (data.data.length > 0) {
					$scope.listData = data;
				} else
					$scope.listData = new Object();
				$scope.input.nPage = 1;
			}, function () {
				$scope.$broadcast('scroll.refreshComplete');
				$timeout(function () {
					$scope.isLoad = false;

				}, 1500);
			});
		};
		//上拉加载更多必须定义
		//		$scope.isLoad = false;
		//加载更多
		$scope.doRefreshMore = function () {
			$scope.isLoad = true;
			if ($scope.listData.data == undefined)
				$scope.input.nPage = 0;
			else
				if ($scope.listData.page.count <= $scope.listData.data.length) {
					$scope.$broadcast('scroll.infiniteScrollComplete');
					if ($scope.listData.page.count < $scope.input.nPage * 20) {
						$scope.input.nPage -= 1;
						$scope.count = $scope.input.nPage * 20;
					} else {
						$scope.count = null;
					}
				}
			Unsalablepro.get_list($scope.input.nPage + 1, $scope.input.keywords, $scope.order, $scope.region, $scope.store_cat, function (data) {
				if (data.data.length > 0) {
					if ($scope.listData.data != undefined) {
						if ($scope.count != null)
							$scope.listData.data.splice($scope.count, $scope.listData.page.count - $scope.count);
						$scope.listData.data = $scope.listData.data.concat(data.data);
						$scope.input.nPage += 1;
					} else {
						$scope.listData = data;
						$scope.input.nPage = 1;
					}

				}
			}, function () {
				$scope.$broadcast('scroll.infiniteScrollComplete');
				$timeout(function () {
					$scope.isLoad = false;

				}, 1500);
			});

		};
		$scope.onShow_detail = function (goods_id) {
			GoPage.go_states("productdetail", { "goods_id": goods_id, "type": "goods" })
		}
		$scope.onshow_first = function (store_id) {
			GoPage.go_states("first", { store_id: store_id });
		}
	}

	controller.$inject = ['$scope', 'Unsalablepro', 'GoPage','GoDialog', '$window', 'GoAddress', 'GoCatTypeAuth', '$timeout', '$ionicSideMenuDelegate'];

	return app.registerController("unsalableproctrl", controller);
});